package zozopo.manual.operations;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;

import zozopo.operations.comm.OperatingService;

public class BrandlogoOS extends OperatingService{
	private static Map<String,BrandlogoTB> brandlogoMap;
	
	public BrandlogoTB getBrandLogo(String brand){
		
		if(brandlogoMap == null){
			initBrandLogo();
		}
		brand = brand.toLowerCase();
		return brandlogoMap.get(brand);
	}
	
	public void initBrandLogo(){
			brandlogoMap = new HashMap<String,BrandlogoTB>();
			SqlMachine sqlMachine = new SqlMachine(conn);
			ResultSet resultSet = sqlMachine.selectAllRecord(BrandlogoTB.TABLE_NAME);
			                                                                                             
			try {
				while(resultSet.next()){
					BrandlogoTB brandlogo = new BrandlogoTB();
					brandlogo.setId_brandlogo(resultSet.getString(BrandlogoTB.ID_BRANDLOGO));
					brandlogo.setBiglogo_brandlogo(resultSet.getString(BrandlogoTB.BIGLOGO_BRANDLOGO));
					brandlogo.setDes_brandlogo(resultSet.getString(BrandlogoTB.DES_BRANDLOGO));
					brandlogo.setLogo_brandlogo(resultSet.getString(BrandlogoTB.LOGO_BRANDLOGO));
					brandlogo.setMinilogo_brandlogo(resultSet.getString(BrandlogoTB.MINILOGO_BRANDLOGO));
					brandlogo.setName_brandlogo(resultSet.getString(BrandlogoTB.NAME_BRANDLOGO));
					brandlogo.setNaming_brandlogo(resultSet.getString(BrandlogoTB.NAMING_BRANDLOGO));
					brandlogoMap.put(brandlogo.getNaming_brandlogo(), brandlogo);
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		
	}
	
	
	public BrandlogoOS(Connection conn){
		super(conn);
		
	}
	
	public BrandlogoOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	public List<BrandlogoTB> getBrandLogoByBrandList(List<String> brandList){
		SqlMachine sqlMachine = new SqlMachine(conn);
		//ResultSet resultSet = sqlMachine.QueryRow(BrandlogoTB.TABLE_NAME, BrandlogoTB.NAMING_BRANDLOGO, brandList,beginPageNum,pageSize);
		//ResultSet resultSet = sqlMachine.QueryRow(BrandlogoTB.TABLE_NAME, BrandlogoTB.NAMING_BRANDLOGO, brandList);
		
		//Map<String,BrandlogoTB> brandlogoMap = getBrandMapByResultSet(resultSet);
		
		List<BrandlogoTB> bakBrandlogo = new ArrayList<BrandlogoTB>();
		
		for(String brand:brandList){
			if(brand.trim().equals("")){
				continue;
			}
			brand = brand.toLowerCase();
			BrandlogoTB brandlogo = new BrandlogoTB();
			if(getBrandLogo(brand) != null){
				brandlogo=getBrandLogo(brand);
				brandlogo.setName_encode(brand);
				brandlogo.setNaming_encode(brand);
			}else{
				brandlogo.setNaming_brandlogo(brand);
				brandlogo.setName_brandlogo(brand);
				brandlogo.setName_encode(brand);
				brandlogo.setNaming_encode(brand);
				brandlogo.setMinilogo_brandlogo("brand/noicon.gif");
			}
			
			bakBrandlogo.add(brandlogo);
			
		}
		
		
		return bakBrandlogo;
	}
	
	public List<BrandlogoTB> getBrandLogoList(){
		SqlMachine sqlMachine = new SqlMachine(conn);
		//ResultSet resultSet = sqlMachine.QueryRow(BrandlogoTB.TABLE_NAME, BrandlogoTB.NAMING_BRANDLOGO, brandList,beginPageNum,pageSize);
		List<BrandlogoTB> brandlogoList = null;
			
		ResultSet resultSet =sqlMachine.distinctRecord(BrandlogoTB.TABLE_NAME, BrandlogoTB.NAME_BRANDLOGO);
		
		if(resultSet != null){
			try {
				brandlogoList = new ArrayList<BrandlogoTB>();
				while(resultSet.next()){
					BrandlogoTB brandlogo = new BrandlogoTB();
				
					String brand = resultSet.getString(BrandlogoTB.NAME_BRANDLOGO);
					
					if(getBrandLogo(brand) != null){
						brandlogo=getBrandLogo(brand);
					}else{
						brandlogo.setNaming_brandlogo(brand);
						brandlogo.setName_brandlogo(brand);
						brandlogo.setMinilogo_brandlogo("brand/noicon.gif");
					}
					
					brandlogoList.add(brandlogo);
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
		
		return brandlogoList;
	}

	
	/**
	 * @version 1.0
	 * @param String brand
	 * @author Shea
	 * @return ResultSet  
	 * @throws IOException 
	 * @see selectRelatedBrand(String brand,int beginPageNum,int pageSize)
	 */
	public List<BrandlogoTB> getBrandListBySql(String sql){
		List<BrandlogoTB> brandlogoList = null;

		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
				try {
					brandlogoList = new ArrayList<BrandlogoTB>();
					while(resultSet.next()){
						BrandlogoTB brandlogo = new BrandlogoTB();
						brandlogo.setId_brandlogo(resultSet.getString(BrandlogoTB.ID_BRANDLOGO));
						brandlogo.setBiglogo_brandlogo(resultSet.getString(BrandlogoTB.BIGLOGO_BRANDLOGO));
						brandlogo.setDes_brandlogo(resultSet.getString(BrandlogoTB.DES_BRANDLOGO));
						brandlogo.setLogo_brandlogo(resultSet.getString(BrandlogoTB.LOGO_BRANDLOGO));
						brandlogo.setMinilogo_brandlogo(resultSet.getString(BrandlogoTB.MINILOGO_BRANDLOGO));
						brandlogo.setName_brandlogo(resultSet.getString(BrandlogoTB.NAME_BRANDLOGO));
						brandlogo.setNaming_brandlogo(resultSet.getString(BrandlogoTB.NAMING_BRANDLOGO));
						brandlogoList.add(brandlogo);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return brandlogoList;
	}
	
	/**
	 * @version 1.0
	 * @param String brand
	 * @author Shea
	 * @return ResultSet  
	 * @throws IOException 
	 * @see selectRelatedBrand(String brand,int beginPageNum,int pageSize)
	 */
	public List<BrandlogoTB> getBrandListByResultSet(ResultSet resultSet){
		List<BrandlogoTB> brandlogoList = null;
		if(resultSet != null){
				try {
					brandlogoList = new ArrayList<BrandlogoTB>();
					while(resultSet.next()){
						BrandlogoTB brandlogo = new BrandlogoTB();
						brandlogo.setId_brandlogo(resultSet.getString(BrandlogoTB.ID_BRANDLOGO));
						brandlogo.setBiglogo_brandlogo(resultSet.getString(BrandlogoTB.BIGLOGO_BRANDLOGO));
						brandlogo.setDes_brandlogo(resultSet.getString(BrandlogoTB.DES_BRANDLOGO));
						brandlogo.setLogo_brandlogo(resultSet.getString(BrandlogoTB.LOGO_BRANDLOGO));
						brandlogo.setMinilogo_brandlogo(resultSet.getString(BrandlogoTB.MINILOGO_BRANDLOGO));
						brandlogo.setName_brandlogo(resultSet.getString(BrandlogoTB.NAME_BRANDLOGO));
						brandlogo.setNaming_brandlogo(resultSet.getString(BrandlogoTB.NAMING_BRANDLOGO));
						brandlogoList.add(brandlogo);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return brandlogoList;
	}
	
	/**
	 * @version 1.0
	 * @param String brand
	 * @author Shea
	 * @return ResultSet  
	 * @throws IOException 
	 * @see selectRelatedBrand(String brand,int beginPageNum,int pageSize)
	 */
	public Map<String,BrandlogoTB> getBrandMapByResultSet(ResultSet resultSet){
		Map<String,BrandlogoTB> brandlogoMap = null;
		if(resultSet != null){
				try {
					brandlogoMap = new HashMap<String,BrandlogoTB>();
					while(resultSet.next()){
						BrandlogoTB brandlogo = new BrandlogoTB();
						brandlogo.setId_brandlogo(resultSet.getString(BrandlogoTB.ID_BRANDLOGO));
						brandlogo.setBiglogo_brandlogo(resultSet.getString(BrandlogoTB.BIGLOGO_BRANDLOGO));
						brandlogo.setDes_brandlogo(resultSet.getString(BrandlogoTB.DES_BRANDLOGO));
						brandlogo.setLogo_brandlogo(resultSet.getString(BrandlogoTB.LOGO_BRANDLOGO));
						brandlogo.setMinilogo_brandlogo(resultSet.getString(BrandlogoTB.MINILOGO_BRANDLOGO));
						brandlogo.setName_brandlogo(resultSet.getString(BrandlogoTB.NAME_BRANDLOGO));
						brandlogo.setNaming_brandlogo(resultSet.getString(BrandlogoTB.NAMING_BRANDLOGO));
						brandlogoMap.put(brandlogo.getNaming_brandlogo(), brandlogo);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return brandlogoMap;
	}
	
	/**
	 * @version 1.0
	 * @param String brand
	 * @author Shea
	 * @return ResultSet  
	 * @throws IOException 
	 * @see 
	 */
	public List<BrandlogoTB> getBrandListByFirstCharacter(String firstChar){
		List<BrandlogoTB> brandlogoList = null;
		
		String sql = "select DISTINCT("+BrandlogoTB.NAME_BRANDLOGO+") from "+BrandlogoTB.TABLE_NAME+" where upper(substring("+BrandlogoTB.NAMING_BRANDLOGO+" , 1 , 1)) = '"+firstChar+"'";
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
				try {
					brandlogoList = new ArrayList<BrandlogoTB>();
					while(resultSet.next()){
						BrandlogoTB brandlogo = new BrandlogoTB();
						
						brandlogo.setName_brandlogo(resultSet.getString(BrandlogoTB.NAME_BRANDLOGO));
						brandlogoList.add(brandlogo);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return brandlogoList;
	}
}
